function o(t, n) {
  var r = 1 * n.charAt(0);
  if (isNaN(r)) return '';
  if (((r = 1 * n.substr(1, r)), isNaN(r))) return '';
  var e,
    o,
    i = n.length,
    u = [],
    c = String(r).length + 1,
    a = function (r) {
      return t.indexOf(n.charAt(r));
    },
    f = t.length;
  if (i !== c + r) return '';
  for (; c < i; )
    (o =
      (e = a(c++)) < 5
        ? e * f + a(c)
        : (e - 5) * f * f + a(c) * f + a((c += 1))),
      (u[u.length] = String.fromCharCode(o)),
      c++;
  return u.join('');
}

function e(t) {
  var n = '8AMN6Z9EFGd23hijkHI4eJfgVrBC7DLO5stuvwWqKPSTUXYabclmnopQRxyz01';
  if (!t) return n;
  for (var r = 0; r < t.length; r++) {
    var e = +t[r],
      o = n.substr(e, 5);
    n = n.replace(o, '') + o;
  }
  return n;
}

// 2. 如果需要，编写锚点函数。通常，可以使用默认锚点，然后通过快捷键动态添加锚点
// 注意，锚点左边为相对宽高的百分比小数（0-1之间的小数）

// 3. 注册图形
// 参数 {key: fn}。key为图形唯一name，否则覆盖原来图形，fn为相关函数
export const registerDraw = (u) => {
  const anchorsFn = {};
  const nodesFn = {};
  let c = void 0;
  if (u.anchorsFn) {
    var a = o(e(undefined), u.anchorsFn);
    a = (a = (a = (a = (a = a.split('(new Point(').join('({'))
      .split('node.rect.x+')
      .join('x:'))
      .split('node.rect.y+')
      .join('y:'))
      .split('*node.rect.width')
      .join(''))
      .split('*node.rect.height')
      .join('');
    for (var f = 0, s = 0; s < 100; s++)
      a = (a = (a = (a = a.replace(',0)', ',id:"'.concat(f++, '"}'))).replace(
        ',1)',
        ',id:"'.concat(f++, '"}'),
      )).replace(',2)', ',id:"'.concat(f++, '"}'))).replace(
        ',3)',
        ',id:"'.concat(f++, '"}'),
      );
    c = new Function('node', a);
  }

  if (u.drawFn) {
    var l = o(e(undefined), u.drawFn);
    l = (l = (l = l.split('font.color').join('textColor'))
      .split('.rect.')
      .join('.calculative.worldRect.'))
      .split('node.fillStyle')
      .join('node.background');
    nodesFn[u.fullname] = new Function('ctx', 'node', l);
    anchorsFn[u.fullname] = c;
  }

  window.meta2d.registerCanvasDraw(nodesFn);
  window.meta2d.registerAnchors(anchorsFn);
};
